class PubSub {

    constructor() {
      this.subscribers = {};
    }
  
    subscribe(type, fn) {
      let listeners = this.subscribers[type] = this.subscribers[type] || [];
      listeners.push(fn);
    }
  
    unsubscribe(type, fn) {
      let listeners = this.subscribers[type];
      if (!listeners) return;
      this.subscribers[type] = listeners.filter(listener => listener !== fn);
    }
  
    publish(type, args) {
      let listeners = this.subscribers[type];
      if (!listeners) return;
      listeners.forEach(listener => listener(args));
    }
  
  }
  